package com.exsun.ex1077.ui.activity.search

import androidx.lifecycle.MutableLiveData
import com.exsun.ex1077.app.Constant
import com.exsun.ex1077.model.Device
import com.exsun.ex1077.network.apiService
import com.exsun.ex1077.network.entity.DeviceDeviceNoSearch
import com.exsun.ex1077.network.entity.DevicePersonSearch
import com.exsun.ex1077.network.entity.MapSearch
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.IntObservableField
import com.zhangping.fastjetpack.extend.databind.StringObservableField

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2022/8/9 10:29
 */
class SearchViewModel: BaseViewModel() {

    val pageType = IntObservableField(Constant.SEARCH_TYPE_MAP)
    var objectType = Constant.OBJECT_TYPE_VEHICLE
    var companyId: String = ""
    val searchWord = StringObservableField()

    val mapSearchResult = MutableLiveData<ResultState<List<MapSearch>?>>()
    val vehicleNoSearchResult = MutableLiveData<ResultState<List<Device>>>()
    val personSearchResult = MutableLiveData<ResultState<List<Device>>>()

    fun loadData(){
        when(pageType.get()){
            Constant.SEARCH_TYPE_MAP -> {
                request({ apiService.mapSearch(searchWord.get())}, mapSearchResult)
            }
            Constant.SEARCH_TYPE_DEVICE_OBJECT -> {
                request({ apiService.deviceObjectNameSearch(searchWord.get(), objectType, companyId)}, vehicleNoSearchResult)
            }
            Constant.SEARCH_TYPE_DEVICE_PERSON -> {
                request({ apiService.devicePersonSearch(searchWord.get(), companyId)}, personSearchResult)
            }
        }
    }

    fun objectNameCheck(): MutableLiveData<Boolean> {
        val liveData = MutableLiveData<Boolean>()
        request({ apiService.deviceObjectNameCheck(searchWord.get(), objectType, companyId)}, {
            liveData.value = true
        })
        return liveData
    }
}